package com.zndroid.gridview.inner.drag;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.NonNull;

import com.zndroid.gridview.AGridDragAdapter;
import com.zndroid.gridview.GridDroppableLayout;
import com.zndroid.gridview.IImageBean;
import com.zndroid.gridview.ImagePreviewActivity;
import com.zndroid.gridview.inner.ScreenUtil;
import com.zndroid.gridview.preview.ImageAttribute;
import com.zndroid.gridview.preview.indicator.impl.RoundLinesIndicator;

import java.util.ArrayList;
import java.util.List;

public class CanPreviewItemClickListener<T extends IImageBean> implements AdapterView.OnItemClickListener {
    private GridDroppableLayout layout;
    private AGridDragAdapter adapter;
    private Context context;
    private int statusHeight;


    public CanPreviewItemClickListener(@NonNull Context context, @NonNull GridDroppableLayout layout, @NonNull AGridDragAdapter<T> adapter) {
        this.layout = layout;
        this.adapter = adapter;
        this.context = context;
        statusHeight = new ScreenUtil().getStatusHeight(context);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //过滤掉固定桩(如果其他特殊需求，请重新实现，本样例默认过滤掉固定桩的预览)
        if (adapter.isFixed(position)) {return;}
        itemClick(adapter.getFinalResult(), view, position);
    }

    private void itemClick(List<T> imageInfoList, View v, int index) {
        int len = imageInfoList.size();

        List<ImageAttribute> imageAttributes = new ArrayList<>(4);
        for (int i = 0; i < len; i++) {
            T info = imageInfoList.get(i);
            View view = layout.getChildAt(i);

            int[] w_h_x_y = new int[4];
            w_h_x_y[0] = view.getWidth();
            w_h_x_y[1] = view.getHeight();
            int[] points = new int[2];
            v.getLocationInWindow(points);
            w_h_x_y[2] = points[0];
            w_h_x_y[3] = points[1] - statusHeight;

            imageAttributes.add(new ImageAttribute(w_h_x_y, info));
        }

//        ImagePreviewActivity.toPreview(context, layout.getImageLoader(), imageAttributes, index);
        ImagePreviewActivity.toPreview(context, new RoundLinesIndicator(), layout.getImageLoader(), imageAttributes, index);
    }
}
